package com.rtx.wms.std.express.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.rtx.edi.model.express.po.PlExpressApiConfig;
import com.rtx.wms.std.express.bean.ExpressOrders;

@Mapper
public interface ExpressMapper{
	
	/**
	 * 	抓取待获取快递单号数据
	 *  udf1 为配置的 承运商代码
	 *  udf2 为配置的 平台代码
	 *  udf3 为配置的 货主代码
	 * @return
	 */
	@Select("SELECT whseid, orderkey FROM orders where carriercode =#{carriercode} and rtxsalesp =#{rtxsalesp} and storerkey =#{storerkey} and expressflag IN ('P','F') AND STATUS IN ('02', '04', '06', '09')")
	List<ExpressOrders> queryOrdersKeysByConfig(PlExpressApiConfig mapping);

	/**
	 * 	抓取待获取快递单号数据
	 * @return
	 */
	@Select("SELECT whseid, orderkey FROM orders o WHERE expressflag IN ('P','F') AND STATUS IN ('02', '04', '06', '09') AND STATUS NOT IN ('98', '99') AND ( rtxexpresskey = '' OR rtxexpresskey IS NULL ) and expressqueue < 10 AND NOT EXISTS ( SELECT 1 FROM wavedetail w WHERE w.orderkey = o.orderkey )")
	List<ExpressOrders> queryOrdersKeys();
	
	/**
	 *  jitx的重新获取单号
	 * @return
	 */
	@Select("SELECT whseid, orderkey FROM orders o WHERE expressflag = 'P' AND STATUS IN ('02', '04', '06', '09') and Xbsjitxflag = 'true' AND STATUS NOT IN ('98', '99') AND NOT EXISTS ( SELECT 1 FROM wavedetail w WHERE w.orderkey = o.orderkey )")
	List<ExpressOrders> queryOrdersJitxKeys();
	
	/**
	 * jitx 获取打印数据
	 * @return
	 */
	@Select("SELECT whseid, orderkey FROM orders o WHERE expressflag IN ('P','F')  AND STATUS NOT IN ('98', '99') AND rtxsalesp='WPH'  and not EXISTS(select 1 from pl_express_result e where e.orderkey = o.ORDERKEY) and expressqueue < 10")
	List<ExpressOrders> queryOrdersjitxPrintData();
	
	
	/**
	 * 	更新状态
	 * @return
	 */
	@Update("update orders set expressflag =#{status},expressmsg=#{msg},expressqueue=expressqueue+1 where orderkey =#{orderkey}")
	int updExpressStatus(@Param("status")String status,@Param("msg")String msg,@Param("orderkey")String orderkey);
	
	/**
	 * 	更新快递单号以及打印报文其他信息
	 * @return
	 */
	@Update("update orders set rtxexpresskey =#{rtxexpresskey},printdate=#{printdate} where orderkey =#{orderkey}")
	int updExpressData(@Param("rtxexpresskey")String rtxexpresskey,@Param("printdate")String printdate,@Param("orderkey")String orderkey);
}
